﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;

namespace ScrollPP.NetService
{
    internal static class Program
    {
        //Default-Service settings
        private const string ServiceName = "ScrollPP.NetService";
        private const string ServiceDisplayName = "Scroll++ NetService";
        private const ServiceBootFlag ServiceStartType = ServiceBootFlag.AutoStart;
        private const string ServiceDependencies = "";
        private const string ServiceAccount = "LocalSystem";
        private const string ServicePassword = null;

        private static void Main()
        {
            Console.WriteLine("start");
            var args = Environment.GetCommandLineArgs();
            if (args.Length > 1)
            {
                if (args[1].Equals("-install", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Installing the Service...");
                    try
                    {
                        ServiceInstaller.InstallAndStart(
                            ServiceName,
                            ServiceDisplayName,
                            Assembly.GetExecutingAssembly().Location,
                            ServiceAccount,
                            ServicePassword,
                            ServiceStartType);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("Installation failed: ");
                        Console.WriteLine(ex.ToString());
                        Console.ReadKey();
                        Environment.Exit(-1);
                    }
                    Console.WriteLine("Installed ScrollPP-Service.");
                }
                else if (args[1].Equals("-uninstall", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Uninstalling the Service...");
                    try
                    {
                        ServiceInstaller.Uninstall(ServiceName);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("Failed to uninstall the Service: ");
                        Console.WriteLine(ex.ToString());
                        Console.ReadKey();
                        Environment.Exit(-1);
                    }
                    Console.WriteLine("Uninstalled ScrollPP-Service.");
                }
                else
                {
                    PrintUsing();
                }
            }
            else
            {
#if DEBUG
                while (!Debugger.IsAttached) ;
                Debugger.Break();
#endif

                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new ScrollPPService() 
                };
                ServiceBase.Run(ServicesToRun);
            }
        }

        private static void PrintUsing()
        {
            Console.WriteLine("Scroll++ by Florian R.");
            Console.WriteLine("Using:");
            Console.WriteLine("\t -install: Install the Service using the default settings.");
            Console.WriteLine("\t -uninstall: Uninstall the Service.");
        }
    }
}
